﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using InvoiceInvoker.Logic;

namespace InvoiceInvoker.MvcUI.Filters
{
	public class PatternsFilter
	{
		[DisplayName("Klient")]
		public string Customer { get; set; }

		[DisplayName("Produkty")]
		public string ProductNames { get; set; }

		public Func<InvoicePattern, bool> GetExpression()
		{
			string customer = Customer ?? "";
			string products = ProductNames ?? "";

			string[] separator = { ", ", "," };
			List<string> productNames = products.ToLower().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();

			return pattern =>
				(
				pattern.RegisteredCustomer.CustomerName.ToLower().Split(' ').Any(x => x.StartsWith(customer.ToLower())) ||
				pattern.RegisteredCustomer.CompanyName.ToLower().StartsWith(customer.ToLower())
				)
				&&
				productNames.All(productName =>
					pattern.InvoicePatternRegisteredProducts.Any(x => x.RegisteredProduct.Name.ToLower().StartsWith(productName)));
		}
	}
}